package com.example.controller;

import com.example.constant.MessageConstant;
import com.example.context.BaseContext;
import com.example.dto.LoginDTO;
import com.example.dto.PageDTO;
import com.example.dto.RegisterDTO;
import com.example.dto.UserDTO;
import com.example.entity.User;
import com.example.properties.JwtProperties;
import com.example.query.UserQuery;
import com.example.result.Result;
import com.example.service.IUserService;
import com.example.utils.JwtUtil;
import com.example.vo.LoginVO;
import com.example.vo.UserVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/user")
@Slf4j
@RequiredArgsConstructor
public class UserController {

    private final IUserService userService;
    private final JwtProperties jwtProperties;

    /*
    * 用户登录
    * @param loginDTO
    * @return
    * */
    @PostMapping("/login")
    public Result<LoginVO> login(@RequestBody LoginDTO loginDTO){
        log.info("用户登录:" + loginDTO);
        //登录验证
        User user = userService.login(loginDTO);
        //生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put("id", user.getId());
        claims.put("username", user.getUsername());
        String token = JwtUtil.createJWT(jwtProperties.getSecretKey(), jwtProperties.getTtl(), claims);
        //封装返回结果
        LoginVO loginVO = LoginVO.builder()
                .id(user.getId())
                .userName(user.getUsername())
                .token(token)
                .build();
        return Result.success(loginVO);
    }

    /*
    * 用户注册
    * @param registerDTO
    * @return
    * */
    @PostMapping("/register")
    public Result register(@RequestBody RegisterDTO registerDTO){
        log.info("用户注册:" + registerDTO);
        userService.register(registerDTO);
        return Result.success();
    }

    /*
    * 用户退出登录
    * @return
    * */
    @PostMapping("/logout")
    public Result logout(){
        return Result.success();
    }

    /*
    * 用户信息更新
    * @param userdto
    * @return
    * */
    @PutMapping
    public Result update(@RequestBody UserDTO userDTO){
        log.info("用户信息更新:" + userDTO);
        userService.updateUser(userDTO);
        return Result.success();
    }

    /*
    * 用户信息查询(分页)
    * @return
    * */
    @GetMapping("/page")
    public Result<PageDTO<UserVO>> page(UserQuery userQuery){
        log.info("用户信息分页查询:" + userQuery);
        return userService.QueryUserpage(userQuery);
    }

    /*
    * 用户删除
    * @param id
    * @return
    * */
    @DeleteMapping
    public Result delete(Long id){
        log.info("用户删除:" + id);
        userService.removeById(id);
        return Result.success();
    }

    /*
    * 查询当前用户信息
    * @param id
    * @return
    * */
    @GetMapping("/info")
    public Result info(){
        Long id = BaseContext.getCurrentId();
        log.info("查询当前用户信息:" + id);
        User user = userService.getById(id);
        return Result.success(user);
    }

        /*
    *根据id查询当前用户信息
    * @param id
    * @return
    * */
    @GetMapping("/{id}")
    public Result QueryById(@PathVariable Long id){
        log.info("查询当前用户信息:" + id);
        User user = userService.getById(id);
        return Result.success(user);
    }

}
